namespace SmartBoard.Core.Command
{
    public abstract class UIBinderRemoteCommandBase:UIBinderCommandBase,IRemoteCommandInternal
    {
        private StreamingContext _context;

        protected override void OnExecutionDone(object parameter)
        {
            _context.Write(parameter, Descriptor.CommandId);
            base.OnExecutionDone(parameter);
        }

        StreamingContext IRemoteCommandInternal.Context
        {
            get { return _context; }
        }

        void IRemoteCommandInternal.SetStreamingContext(StreamingContext context)
        {
            if (_context != null)
            {
                _context.RemoteActionExecuted -= HandleRemoteActionExecuted;
            }

            _context = context;

            if (_context != null)
            {
                _context.RemoteActionExecuted += HandleRemoteActionExecuted;
            }
        }

        private void HandleRemoteActionExecuted(object sender, RemoteActionExecuteEventArgs e)
        {
            this.DoExecution(e.Parameter);
        }
    }
}